/*
 * TNetworkAccessManager.cpp
 *
 *  Created on: 2012-10-30
 *      Author: minhua
 */

#include "TNetworkAccessManager.h"

#include <QNetworkProxy>
#include <QMutexLocker>

TNetworkAccessManager::TNetworkAccessManager()
{

}

TNetworkAccessManager::~TNetworkAccessManager()
{
	clearManagerList();
}

TNetworkAccessManager *TNetworkAccessManager::instance()
{
	static TNetworkAccessManager accessmanager;
	return &accessmanager;
}

void TNetworkAccessManager::setSession(TSessionPointer session)
{
	clearManagerList();
	TCriticalSectionLocker locker(&_criticalSection);
	_session = session;
	return;

	int i;
	QNetworkAccessManager *networkManager;
	for (i=0; i<5 && _session != NULL; i++)
	{
		networkManager = new QNetworkAccessManager(this);
		connect(networkManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(dataDone(QNetworkReply*)));
		_networkManagerEmptyList.push_back(networkManager);
	}

}

void TNetworkAccessManager::clearManagerList()
{
	TCriticalSectionLocker locker(&_criticalSection);
	int i;
	for(i=0; i<_networkManagerEmptyList.length(); i++)
	{
		delete _networkManagerEmptyList[i];
	}

	_networkManagerEmptyList.clear();
}

QNetworkReply *TNetworkAccessManager::post(QNetworkRequest &request, const QByteArray &data)
{
	TCriticalSectionLocker locker(&_criticalSection);
	request.setRawHeader("Connection", QString("keep-alive").toAscii());
	request.setRawHeader("Keep-Alive", QString("timeout=600, max=5000").toAscii());
	QNetworkAccessManager *networkManager;
	if (_networkManagerEmptyList.length()>0)
	{
		networkManager = _networkManagerEmptyList.front();
		_networkManagerEmptyList.pop_front();
	}
	else
	{
		networkManager = new QNetworkAccessManager(this);
		connect(networkManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(dataDone(QNetworkReply*)));
	}
	QNetworkReply *reply = networkManager->post(request, data);
	return reply;
}

QNetworkReply *TNetworkAccessManager::post(QNetworkRequest &request, QIODevice *data)
{
	TCriticalSectionLocker locker(&_criticalSection);
	request.setRawHeader("Connection", QString("keep-alive").toAscii());
	request.setRawHeader("Keep-Alive", QString("timeout=600, max=5000").toAscii());
	QNetworkAccessManager *networkManager;
	if (_networkManagerEmptyList.length()>0)
	{
		networkManager = _networkManagerEmptyList.front();
		_networkManagerEmptyList.pop_front();
	}
	else
	{
		networkManager = new QNetworkAccessManager(this);
		connect(networkManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(dataDone(QNetworkReply*)));
	}
	QNetworkReply *reply = networkManager->post(request, data);
	return reply;
}

void TNetworkAccessManager::dataDone(QNetworkReply *reply)
{
	emit done(reply);
	TCriticalSectionLocker locker(&_criticalSection);
	if(reply->error()==QNetworkReply::NoError)
	{
		_networkManagerEmptyList.push_back(reply->manager());
	}
	else
	{
		int i;
		for (i=0; i<_networkManagerEmptyList.length(); i++)
		{
			if (reply->manager() == _networkManagerEmptyList[i])
			{
				_networkManagerEmptyList.erase(_networkManagerEmptyList.begin() + i);
				break;
			}
		}
		reply->manager()->deleteLater();
	}
}
